package com.sun.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {

    private String TAG = "HomeActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        List<TextView> textViews = new ArrayList<>();
        textViews.add(findViewById(R.id.place_1));
        textViews.add(findViewById(R.id.place_2));
        textViews.add(findViewById(R.id.place_3));
        textViews.add(findViewById(R.id.place_4));
        textViews.get(0).setBackgroundColor(R.color.black);

        ViewPager viewPager = findViewById(R.id.view_pager);
        //获取这个activity的渲染器
        LayoutInflater lf = LayoutInflater.from(this);
        View pager1 = lf.inflate(R.layout.pager_1, null);
        View pager2 = lf.inflate(R.layout.pager_2, null);
        View pager3 = lf.inflate(R.layout.pager_3, null);
        View pager4 = lf.inflate(R.layout.pager_4, null);
        //存viewPagers
        List<View> viewPagers = new ArrayList<>();
        viewPagers.add(pager1);
        viewPagers.add(pager2);
        viewPagers.add(pager3);
        viewPagers.add(pager4);
        //应用Adapter
        viewPager.setAdapter(new ViewPagerAdapter(viewPagers));
        //添加切换事件
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.i(TAG, "onPageScrolled: ");
            }

            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "onPageSelected: ");
                for (int i = 0; i < textViews.size(); i++) {
                    if (position == i) textViews.get(i).setBackgroundColor(R.color.black);
                    else textViews.get(i).setBackgroundColor(0);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.i(TAG, "onPageScrollStateChanged: ");
            }
        });

    }
}